<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\matrix\controller;
use think\Db;
use cmf\controller\RestBaseController;
use think\db\connector\Sqlite;
use think\response\Xml;

/**
 * 微信机器人  同步群消息
 * Class WxMsgController
 * http://192.168.2.219/api/matrix/wx_msg/send.html
 * @package plugins\matrix\controller
 */
class WxMsgController extends RestBaseController
{
    /**
     * 发送消息
     */
    public function send()
    {
        $msgSvrId = $_POST["msgSvrId"];
        $type = $_POST["type"];
        $talker = $_POST["talker"];
        $content = $_POST["content"];
        if(empty($msgSvrId) || empty($type) || empty($talker) || empty($content)){
            return $this->error("parameter is not empty!");
        }else{
            if($type == "1"){  //文本
                return $this->add_matrix_msg_text($msgSvrId, $type, $talker, $content);
            }else if($type == "43"){  //视频
                return $this->add_matrix_msg_video($msgSvrId, $type, $talker, $content);
            }else if($type == "49"){  //分享文章
                return $this->add_matrix_msg_share($msgSvrId, $type, $talker, $content);
            }else if($type == "16777265"){  //APP任务分享
                return $this->add_matrix_msg_app_share($msgSvrId, $type, $talker, $content);
            }else if($type == "570425393"){  //加入群组
                return $this->add_matrix_msg_addgroup($msgSvrId, $type, $talker, $content);
            }else{
                return $this->error("type does not exist!");
            }
        }
    }

    //添加微信文本消息  默认:1
    public function add_matrix_msg_text($msgSvrId,$type,$talker,$content){
        $msg = $this->get_matrix_msg($msgSvrId,$type,$talker);
        $i = strpos($content,":");
        $msg["sender"] = trim(substr($content,0, $i));
        $msg["content"] = trim(substr($content,$i+1));
        DB::name('matrix_msg_text')->insert($msg);
        return $this->success("OK");
    }

    //添加微信视频消息  默认:43
    public function add_matrix_msg_video($msgSvrId,$type,$talker,$content){
        $msg = $this->get_matrix_msg($msgSvrId,$type,$talker);
        $i = strpos($content,":");
        $msg["sender"] = trim(substr($content,0, $i));
        DB::name('matrix_msg_video')->insert($msg);
        return $this->success("OK");
    }

    //添加微信分享文章消息 默认:49
    public function add_matrix_msg_share($msgSvrId,$type,$talker,$content){
        $msg = $this->get_matrix_msg($msgSvrId,$type,$talker);
        $i = strpos($content,":");
        $msg["sender"] = trim(substr($content,0, $i));
        $content = trim(substr($content,$i+1));
        $arr = $this->xmlToArray($content);
        $msg["title"] =$arr["appmsg"]["title"];
        $msg["url"] = $arr["appmsg"]["url"];
        DB::name('matrix_msg_share')->insert($msg);
        return $this->success("OK");
    }

    //添加APP分享任务消息
    public function add_matrix_msg_app_share($msgSvrId,$type,$talker,$content){
        $msg = $this->get_matrix_msg($msgSvrId,$type,$talker);
        $i = strpos($content,":");
        $msg["sender"] = trim(substr($content,0, $i));
        $content = trim(substr($content,$i+1));
        $arr = $this->xmlToArray($content);
        $msg["title"] =$arr["appmsg"]["title"];
        DB::name('matrix_msg_app_share')->insert($msg);
        return $this->success("OK",$arr);
    }

    //添加微信加入群组消息  默认:570425393
    public function add_matrix_msg_addgroup($msgSvrId,$type,$talker,$content){
        $msg = $this->get_matrix_msg($msgSvrId,$type,$talker);
        $i = strpos($content,":");
        $content = trim(substr($content,$i+1));
        $arr = $this->xmlToArray($content);
        $info = $arr["sysmsgtemplate"]["content_template"]["link_list"]["link"][0]["memberlist"]["member"];
        $msg["nickname"] =$info["nickname"];
        $msg["sender"] = $info["username"];
        DB::name('matrix_msg_addgroup')->insert($msg);
        return $this->success("OK");
    }

    public function get_matrix_msg($msgSvrId,$type,$talker){
        $msg["type"] = $type;
        $msg["talker"] = $talker;
        $msg["msg_svr_id"] = $msgSvrId;
        $msg["create_time"] = time();
        return $msg;
    }


    function xmlToArray($xml){
        //考虑到xml文档中可能会包含<![CDATA[]]>标签，第三个参数设置为LIBXML_NOCDATA
        if (file_exists($xml)) {
            libxml_disable_entity_loader(false);
            $xml_string = simplexml_load_file($xml,'SimpleXMLElement', LIBXML_NOCDATA);
        }else{
            libxml_disable_entity_loader(true);
            $xml_string = simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA);
        }
        $result = json_decode(json_encode($xml_string),true);
        return $result;
    }


}
